package com.zhaoxi.project.fms.service;

import com.zhaoxi.common.utils.DateUtils;
import com.zhaoxi.framework.web.BaseService;
import com.zhaoxi.framework.web.PageData;
import com.zhaoxi.project.fms.mapper.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 * 账户信息Service业务层处理
 */
@Service
public class AccountService extends BaseService {
  @Autowired
  private AccountMapper accountMapper;

  @Autowired
  private BillService billService;

  //查询数据集合
  public List<PageData> getList(PageData pd) {
    return accountMapper.getList(pd);
  }

  //查询数据对象 通过id
  public PageData getById(PageData pd) {
    PageData pd1 = new PageData();
    if (pd.getLong("id") == null){
      return null;
    }
    pd1.put("id",pd.getLong("id"));
    List<PageData> list = accountMapper.getList(pd1);
    if (list.size() > 0){
      return list.get(0);
    }
    return null;
  }

  //查询数据行数
  public PageData getCount(PageData pd) {

    return this.success(accountMapper.getCount(pd));
  }

  //新增
  public PageData add(PageData pd) {

    return this.responses(accountMapper.add(pd));
  }

  //更新
  public PageData edit(PageData pd) {

    return this.responses(accountMapper.edit(pd));
  }

  //更新currentAmount余额
  @Transactional(propagation = Propagation.REQUIRES_NEW)
  public PageData editForBillExec(PageData pd) {
    PageData bill = billService.getById(pd);
    if (bill == null){
      return this.error("没有查询到该id的财务单据");
    }
    if (!bill.getString("state").equals("5")){
      return this.error("单据状态并不是待执行！！！");
    }
//    取出当前账户信息
    PageData params = new PageData();
    params.put("id",bill.getLong("accountId"));
    PageData account = this.getById(params);
    if (account == null){
      return this.error("没有查询到单据账户信息！！！");
    }
    PageData billType = billService.getTypeById(bill);
    if (!(billType.getString("fmsType").equals("out") || billType.getString("fmsType").equals("in") ) ){
      return this.error("单据单据不是财务类别的！！！");
    }

    double costMoney = bill.getDouble("costMoney");
    if (!(costMoney > 0)){
      return this.error("操作金额应该大于0！！！");
    }
//    准备更新账户余额
    account.put("updateBy",this.getUsername());
    account.put("updateTime",DateUtils.getNowDate());
    PageData res = new PageData();
    if (billType.getString("fmsType").equals("in")){
      account.put("currentAmount",costMoney);
      res = this.responses(accountMapper.editCurrentAmount(account));
    }else if (billType.getString("fmsType").equals("out")){
      if (account.getDouble("currentAmount") < costMoney){
        return this.error("账户余额不足，无法操作！！！");
      }
      account.put("currentAmount",costMoney * -1);
      res = this.responses(accountMapper.editCurrentAmount(account));
    }
    if (res.isError()){
      return res;
    }
    bill.put("operId",this.getUserId());
    bill.put("executionTime", DateUtils.getNowDate());
    bill.put("updateBy",this.getUsername());
    bill.put("money",bill.getDouble("costMoney"));
    bill.put("state","0");
    billService.edit(bill);
    return res;
  }


  //删除
  public PageData del(Long id) {

    return this.responses(accountMapper.del(id));
  }
}
